import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import java.util.Vector;

public class SplashFileList extends Canvas
{

	private MenuManager mgr;
	private Vector fList;
	private int pos;
	private String status;
	private StringBuffer current;
	private SplashFileListThread sflt;
	private String requested;
	
	private Image file;
	private Image folder;
	
	public SplashFileList(MenuManager mm)
	{
		this.mgr = mm;
		this.pos = 0;
		this.fList = new Vector();
		this.status = "Ni datotek";
		
		try
		{
			this.folder = Image.createImage("/folder.png");
			this.file = Image.createImage("/file.png");
		}
		catch(Exception e){}
		
		repaint();
	}
	
	public void load(String url)
	{
		this.sflt = new SplashFileListThread(url, this);
		new Thread(this.sflt).start();
	}

	protected void keyReleased(int keyCode)
	{
		switch(getGameAction(keyCode))
		{
			case Canvas.UP:
				if(pos > 0)
					pos--;
				else
					pos = fList.size() - 1;
			break;
			case Canvas.DOWN:
				if(pos < fList.size() - 1)
					pos++;
				else
					pos = 0;
			break;
			case Canvas.FIRE:
				/* Open folder */
				if(((String)fList.elementAt(pos)).charAt(0) != 'f')
				{
					requested = this.current + "/" + 
						((String)fList.elementAt(pos)).substring(2);
					
					mgr.itemSelected(FormConstants.OK);
				}
				
			break;
		}
		repaint();
	}
	
	protected void keyRepeated(int keyCode)
	{
		switch(getGameAction(keyCode))
		{
			case Canvas.UP:
				if(pos > 0)
					pos--;
				else
					pos = fList.size() - 1;
			break;
			case Canvas.DOWN:
				if(pos < fList.size() - 1)
					pos++;
				else
					pos = 0;
			break;
		}
		repaint();
	}
	
	public String getRequested()
	{
		return this.requested;
	}
	
	public void done()
	{
		this.fList = this.sflt.getRows();
		this.current = this.sflt.current();
		repaint();
	}
	
	public void setTitle(String t)
	{
		this.status = t;
		repaint();
	}
	
	public void paint(Graphics g)
	{
		
		int w = getWidth();
		int h = getHeight();
		int em = 30;
		int emn = h / em;
		
		g.setColor(mgr.getDefaultBGColor());
		g.fillRect(0,0, getWidth(), getHeight());
		
		g.setColor(0x00FFFFFF - g.getColor());
		
		int start = 0;
		int end = emn - 1;
		
		if(pos >= emn - 1)
		{
			end = pos + 2;
			start = end - emn;
		}
	
		if(fList == null || fList.isEmpty())
		{
			g.drawString(status, w / 2, h / 2, Graphics.BASELINE | 
					Graphics.HCENTER);
			return;
		}
		
		//System.out.println("Pos: " + pos + "/" + (fList.size() - 1));
		
		String tmps;
		int size = fList.size();
		for(int i  = 0; i < emn && (start + i) < size; i++)
		{
			tmps = (String)fList.elementAt(start + i);
			if(start + i == pos)
			{
				g.setColor(0x000000FF);
				g.fillRect(0, em * (i + 1) - (em / 2), w, em);
				g.setColor(0x00FFFFFF - mgr.getDefaultBGColor());
			}
			
			g.drawImage((tmps.charAt(0) == 'f') ? file : folder,
						5, em * (i + 1), Graphics.VCENTER | Graphics.LEFT);
			
			if(!tmps.substring(2).equals(".."))
				g.drawString(tmps.substring(2), 30, em * (i + 1), 
									Graphics.BASELINE | Graphics.LEFT);
			else
				g.drawString("<< {Nazaj}", 30, em * (i + 1), 
									Graphics.BASELINE | Graphics.LEFT);
		}
		
	}
}
